﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WillBe.Framework;
using System.Diagnostics;

namespace WillBe.Attributes.Logic
{
    public class WillBeValidatingPerformanceSlaAttributeAction : WillBeActionBase<WillBeValidatingPerformanceSlaAttribute>
    {
        public override void Do()
        {
            var stopWatch = new Stopwatch();
            stopWatch.Start();

            Action.Invoke();

            stopWatch.Stop();

            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            // Format and display the TimeSpan value.
            if(ts > new TimeSpan(Convert.ToInt64(1000*this.Attribute.Seconds)))
            {
                Trace.WriteLine("Elapsed time violated SLA and was : "+ String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                    ts.Hours, ts.Minutes, ts.Seconds,
                    ts.Milliseconds / 10));
            }
        }
    }
}
